﻿using MES_SystemUI.Domain;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MES_SystemUI.UI
{
    public partial class ProductionLinesPutin : MES_SystemUI.UI.Putin
    {
        public ProductionLinesPutin()
        {
            InitializeComponent();
            this.Load += ProductionLinesPutin_Load;
        }

        private void ProductionLinesPutin_Load(object sender, EventArgs e)
        {
            ComboBoxBingData();
            RefreshText();
        }

        private void RefreshText()
        {
            textBox1.Text = string.Empty;
            comboBox1.SelectedIndex = -1;
            comboBox1.Text = string.Empty;
            comboBox2.SelectedIndex = -1;
            comboBox2.Text = string.Empty;
            comboBox3.SelectedIndex = -1;
            comboBox3.Text = string.Empty;
        }

        private void ComboBoxBingData()
        {
            using (var c = new Context.MES_SystemContext())
            {
                comboBox1.DataSource = c.productionLineCategories.ToList();
                comboBox1.DisplayMember = "ProductionLineCategorieName";
                comboBox1.ValueMember = "ProductionLineCategorieId";
                comboBox2.DataSource = c.productionTechnologies.ToList();
                comboBox2.DisplayMember = "ProductionTechnologyName";
                comboBox2.ValueMember = "ProductionTechnologyId";
                comboBox3.DataSource = c.workShops.ToList();
                comboBox3.DisplayMember = "WorkShopName";
                comboBox3.ValueMember = "WorkShopId";
            }
        }
        public override void PutinData()
        {
            if (comboBox1.SelectedIndex == -1)
            {
                MessageBox.Show("产线类型不能为空");
                return;
            }
            if (comboBox2.SelectedIndex == -1)
            {
                MessageBox.Show("作业工艺不能为空");
                return;
            }
            if (comboBox3.SelectedIndex == -1)
            {
                MessageBox.Show("所属车间不能为空");
                return;
            }
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                MessageBox.Show("产线名称不能为空");
                return;
            }
            using (var c = new Context.MES_SystemContext())
            {
                ProductionLine mode = new ProductionLine()
                {
                    ProductionLineName = textBox1.Text,
                    ProductionLineCaregoryId = int.Parse(comboBox1.SelectedValue.ToString()),
                    ProductionTechnologyId = int.Parse(comboBox2.SelectedValue.ToString()),
                    WorkShopId = int.Parse(comboBox3.SelectedValue.ToString()),
                    CreateUserId = SystemDataConfig.LoginUser.UserId,
                };
                c.Entry(mode).State = System.Data.Entity.EntityState.Added;
                c.SaveChanges();
            }
            RefreshText();
            MessageBox.Show("提交成功！");
        }
    }
}
